using System;
using System.Data;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;
using PCSComUtils.DataContext;
using System.Transactions;
using PCSComUtils.DataAccess;
using System.Linq;

namespace PCSComUtils.MasterSetup.BO
{
    /// <summary>
    /// Summary description for PartyBO.
    /// </summary>
    public class PartyBO
    {
        public PartyBO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IObjectBO Members

        public void Add(object pObjectDetail)
        {
            // TODO:  Add PartyBO.Add implementation
        }

        //**************************************************************************              
        ///    <Description>
        ///       This method uses to add data to MST_Party
        ///    </Description>
        ///    <Inputs>
        ///        MST_PartyVO       
        ///    </Inputs>
        ///    <Outputs>
        ///       newly inserted primarkey value
        ///    </Outputs>
        ///    <Returns>
        ///       int
        ///    </Returns>
        ///    <Authors>
        ///       DungLa
        ///    </Authors>
        ///    <History>
        ///       04-Mar-2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public int AddReturnID(object pObjectDetail)
        {
            try
            {
                MST_PartyDS dsParty = new MST_PartyDS();
                return dsParty.AddReturnID(pObjectDetail);
            }
            catch (PCSException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///       This method uses to delete MST_Party
        ///    </Description>
        ///    <Inputs>
        ///        MST_PartyVO       
        ///    </Inputs>
        ///    <Outputs>
        ///       N/A
        ///    </Outputs>
        ///    <Returns>
        ///       N/A
        ///    </Returns>
        ///    <Authors>
        ///       DungLa
        ///    </Authors>
        ///    <History>
        ///       04-Mar-2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public void Delete(object pObjectVO)
        {
            try
            {
                MST_Party voParty = (MST_Party)pObjectVO;
                // delete all contact first
                MST_PartyContactDS dsContact = new MST_PartyContactDS();
                dsContact.DeleteByParty(voParty.PartyID);
                // then delete all location
                MST_PartyLocationDS dsLocation = new MST_PartyLocationDS();
                dsLocation.DeleteByParty(voParty.PartyID);
                // finally delete Party
                MST_PartyDS dsParty = new MST_PartyDS();
                dsParty.Delete(voParty.PartyID);
            }
            catch (PCSException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public MST_Party GetParty(int? iId, string strCode)
        {
            MST_Party obj = null;
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    if (iId != null && strCode != string.Empty)
                        obj = db.MST_Parties.Single(e => e.PartyID == iId && e.Code == strCode);
                    else
                        if (iId != null)
                            obj = db.MST_Parties.Single(e => e.PartyID == iId);
                        else
                            obj = db.MST_Parties.Single(e => e.Code == strCode);
                }
                trans.Complete();
            }
            return obj;
        }
        //**************************************************************************              
        ///    <Description>
        ///       This method uses to update a dataset
        ///    </Description>
        ///    <Inputs>
        ///        DataSet to update
        ///    </Inputs>
        ///    <Outputs>
        ///       N/A
        ///    </Outputs>
        ///    <Returns>
        ///       N/A
        ///    </Returns>
        ///    <Authors>
        ///       DungLa
        ///    </Authors>
        ///    <History>
        ///       04-Mar-2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public void UpdateDataSet(System.Data.DataSet dstData)
        {
            try
            {
                MST_PartyDS dsParty = new MST_PartyDS();
                dsParty.UpdateDataSet(dstData);
            }
            catch (PCSException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///       This method uses to update MST_PartyVO
        ///    </Description>
        ///    <Inputs>
        ///        MST_PartyVO       
        ///    </Inputs>
        ///    <Outputs>
        ///       N/A
        ///    </Outputs>
        ///    <Returns>
        ///       N/A
        ///    </Returns>
        ///    <Authors>
        ///       DungLa
        ///    </Authors>
        ///    <History>
        ///       04-Mar-2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public void Update(object pObjectDetail)
        {
            try
            {
                MST_PartyDS dsParty = new MST_PartyDS();
                dsParty.Update(pObjectDetail);
            }
            catch (PCSException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //[AutoComplete(true)]
        public DataTable GetPartyType()
        {
            MST_PartyDS dsParty = new MST_PartyDS();
            return dsParty.GetPartyType();
        }

        #endregion
        public DataSet ListAll()
        {
            MST_PartyDS dsParty = new MST_PartyDS();
            return dsParty.List();
        }
        //[AutoComplete(true)]
        public DataSet ListVendor()
        {
            MST_PartyDS dsParty = new MST_PartyDS();
            return dsParty.ListVendor();
        }
    }
}
